<template>
	<view class="pages-mine" :style="{background:pageColor}">
		<block v-if="isLoad">
			<!-- #ifndef H5 -->
			<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" :title="navTitle" color="#ffffff"
				:backgroundColor="primaryColor">
			</uni-nav-bar>
			<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
			<!-- #endif -->

			<longbingbing-mine-page-type></longbingbing-mine-page-type>
		</block>
		<view class="space-lg"></view>
		<view :style="{height: `${configInfo.tabbarHeight}px`}"></view>
		<longbingbing-tabbar :cur="5"></longbingbing-tabbar>
		<longbingbing-customer-item></longbingbing-customer-item>


		<!-- #ifdef APP-PLUS -->
		<longbingbing-login-info></longbingbing-login-info>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<user-privacy ref="user_privacy" :show="false"></user-privacy>
		<!-- #endif -->

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import longbingbingMinePageType from "@/components/longbingbing-mine-page-type.vue"
	export default {
		components: {
			longbingbingMinePageType,
		},
		data() {
			return {
				isLoad: false,
				options: {},
				navTitle: ''
			}
		},
		computed: mapState({
			configInfo: state => state.config.configInfo,
			mineInfo: state => state.user.mineInfo,
			haveOperItem: state => state.order.haveOperItem,
		}),
		async onLoad(options) {
			this.options = options
			let {
				type = 1
			} = options
			let {
				id: mine_id = -1
			} = this.mineInfo
			if (mine_id == -1) {
				this.$util.showLoading()
			}
			await this.$util.toAsyAccLogin()
			await this.initIndex()
			this.initNavTitle()
			console.log('====OnLoad  page-mine')
		},
		async onShow() {
			this.initNavTitle()
			if (!this.isLoad) return
			// getWecomStaff 不可修改至toContact调取，小程序不兼容
			await Promise.all([this.getMineInfo(), this.getWecomStaff()])
		},
		async onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			await this.initRefresh()
			uni.stopPullDownRefresh()
		},
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo', 'getMineInfo', 'getCoachInfo', 'getWecomStaff']),
			...mapMutations(['updateUserItem', 'updateOrderItem']),
			async initIndex(refresh = false) {
				if (!this.configInfo.id || refresh || !this.userInfo.id) {
					await this.getConfigInfo()
				}
				Promise.all(this.autograph && !this.userInfo.id ? [this.getUserInfo(), this.getMineInfo()] : [this
					.getMineInfo()
				])
				let {
					coach_status = -1,
						channel_status = -1,
						salesman_status = -1,
						broker_status = -1,
						is_admin = 0,
						is_fx = 0
				} = this.mineInfo
				this.isLoad = true
				let {
					coach_position = 0
				} = this.userCoachStatus
				if (coach_position && coach_status == 2 && this.autograph && this.userInfo.id) {
					this.initLocation()
				}
				this.$util.hideAll()
				await this.getWecomStaff()
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			async initRefresh() {
				await this.initIndex(true)
			},
			initNavTitle() {
				let {
					tabBar
				} = this.configInfo
				let ind = tabBar.findIndex(item => {
					return item.id == 5
				})
				let navTitle = ind == -1 ? '我的' : tabBar[ind]
					.name
				this.navTitle = navTitle
				// #ifdef H5
				uni.setNavigationBarTitle({
					title: navTitle
				})
				// #endif
			},
			// 下拉刷新自动更新地址
			async initLocation() {
				// #ifdef MP-WEIXIN
				let privacyCheck = this.$refs.user_privacy.check()
				if (privacyCheck) {
					this.$refs.user_privacy.open()
					return
				}
				await this.$util.checkAuth({
					type: 'userLocation'
				})
				// #endif
				let {
					lat,
					lng,
					isIp = true
				} = await this.$util.getCoachUtilLoca(false)
				if (lat && lng && !isIp) {
					let {
						address = ''
					} = await this.$util.getMapAddr({
						lat,
						lng
					})
					await this.toUpdateCoachAddr({
						lat,
						lng,
						address
					})
				}
			},
			// 更新技-师地址
			async toUpdateCoachAddr(param) {
				try {
					if (!param.address) {
						delete param.address
					}
					await this.$api.technician.coachUpdate(param)
					let coachInfo = Object.assign({}, this.$util.deepCopy(this.coachInfo), param)
					this.updateUserItem({
						key: 'coachInfo',
						val: coachInfo
					})
				} catch (e) {
					await this.getMineInfo()
				}
			},
		}
	}
</script>

<style lang="scss">
</style>